set more off

use dataset, clear

*** make interactions ***

gen demincumbentXoverridep = demincumbent*overridepassed
gen demincumbentXoverridel = demincumbent*overridelost

forvalues i = 1(1)4 {
	gen demincumbentXl`i'overridep  = demincumbent*l`i'overridep
	gen demincumbentXf`i'overridep  = demincumbent*f`i'overridep
}


*** do the regressions ***

eststo clear

*** baseline ***

gen treatment = demincumbentXoverridep
eststo, title("Estimate from Table 2, Column 1"): xi: reg demvoteshare lagdemvoteshare treatment i.year [aw=totalvotes], cl(town) 

*** with town FE ***

eststo, title("Add town fixed effects"): xi: areg demvoteshare lagdemvoteshare treatment i.year [aw=totalvotes], cl(town) absorb(town)

*** with extra lag  ***

eststo, title("Add eight-year Dem. vote lag"): xi: reg demvoteshare lagdemvoteshare laglagdemvoteshare treatment i.year [aw=totalvotes], cl(town)

*** effect by excluded year ***

preserve
	keep if !mi(demvoteshare) 
	gen B = .
	gen UB = .
	gen LB = .
	gen SE = .
	levelsof year, local(levels)
	
	foreach l of local levels {
		di "`l'"
		xi: reg demvoteshare lagdemvoteshare demincumbentXoverridep  i.year if year != `l' [aw=totalvotes], cl(town) 
		replace B = _b[demincumbentXoverridep] if year == `l'
		replace UB = _b[demincumbentXoverridep]+1.96*_se[demincumbentXoverridep] if year == `l'
		replace LB = _b[demincumbentXoverridep]-1.96*_se[demincumbentXoverridep] if year == `l'		
		replace SE = _se[demincumbentXoverridep] if year == `l'
	}
 
	gen absB = abs(B)
	sort absB
	gen index = _n
	su year if index == 1
	eststo, title("Smallest effect dropping individual years and re-estimating"): xi: reg demvoteshare lagdemvoteshare treatment i.year if year != `r(mean)'  [aw=totalvotes], cl(town) 
restore 

*** effect by excluded county ***

preserve
	keep if !mi(demvoteshare) 
	gen B = .
	gen UB = .
	gen LB = .
	gen SE = .
	levelsof county, local(levels)
	foreach l of local levels {
		di "`l'"
		xi: reg demvoteshare lagdemvoteshare demincumbentXoverridep  i.year if county != "`l'"  [aw=totalvotes], cl(town) 
		replace B = _b[demincumbentXoverridep] if county == "`l'"
		replace UB = _b[demincumbentXoverridep]+1.96*_se[demincumbentXoverridep] if county == "`l'"
		replace LB = _b[demincumbentXoverridep]-1.96*_se[demincumbentXoverridep] if county == "`l'"
		replace SE = _se[demincumbentXoverridep] if county == "`l'"
	}
	gen absB = abs(B)
	sort absB
	gen index = _n
	encode county, gen(cid)
	su cid if index == 1
	eststo, title("Smallest effect dropping individual counties and re-estimating"): xi: reg demvoteshare lagdemvoteshare treatment i.year if cid != `r(mean)'  [aw=totalvotes], cl(town) 
restore
 
*** no school-related votes ***

gen schoolvote = regexm(lower(desc), "school") | regexm(lower(desc), "educ")
eststo, title("Exclude education-related referendums"): xi: reg demvoteshare lagdemvoteshare treatment i.year if schoolvote!=1  [aw=totalvotes], cl(town) 

*** holders only ***

eststo, title("Holders only"): xi: reg demvoteshare lagdemvoteshare treatment i.year if override==1 [aw=totalvotes], cl(town) 

*** placebo: lost override ***

drop treatment
gen treatment = demincumbentXoverridel
eststo, title("Placebo treatment: referendums that failed"): xi: reg demvoteshare lagdemvoteshare treatment i.year  [aw=totalvotes], cl(town) 

*** print table ***

esttab, se nostar keep(treatment) mtitles onecell

local myK = `r(nmodels)'
forvalues k = 1(1)`myK' {
	local mylab`k' = "(`k') " + r(m`k'_estimates_title)

}

matrix C = r(coefs)
eststo clear
local rnames : rownames C
local models : coleq C
local models : list uniq models
local i 0
foreach name of local rnames {
    local ++i
    local j 0
    capture matrix drop b
    capture matrix drop se
    foreach model of local models {
        local ++j
        matrix tmp = C[`i', 2*`j'-1]
        if tmp[1,1]<. {
            matrix colnames tmp = `model'
            matrix b = nullmat(b), tmp
            matrix tmp[1,1] = C[`i', 2*`j']
            matrix se = nullmat(se), tmp
        }
    }
    ereturn post b
    quietly estadd matrix se
    eststo `name'
}


#delimit;

esttab 
	using "table 3.tex"
	,
		replace
		coeflabels(
			est1 "`mylab1'"
			est2 "`mylab2'"
			est3 "`mylab3'"
			est4 "`mylab4'"
			est5 "`mylab5'"
			est6 "`mylab6'"
			est7 "`mylab7'"
			est8 "`mylab8'"
			est9 "`mylab9'"
			est10 "`mylab10'"
			est11 "`mylab11'"
			est12 "`mylab12'"
		)
		noobs
		booktabs
		nonote
		cells(b(fmt(2) star) se(par))
		gap
		nomtitles
		nonum
		unstack
		
		b(2)
		mlabels("Estimate")
		collabels("(s.e.)")
		align(c)
		width(\hsize)
		;

#delimit cr
